/**
 * 封装网络请求
 */
    layui.define(["jquery", "layer"], function (exports) {
        var $ = layui.$,
        layer = layui.layer;

        var baseUrl = 'http://192.168.5.103/index.php/'; //域名地址
         
        const token = sessionStorage.getItem("token"); //获取存储在浏览器中的token

        let headers;

        if(token){
            headers = {
                "Authorization": token
            }
        }

        var miniRequest = {
            /**get请求数据 */
            getData0: function (url = '', params = {}, isLoading = false) {
                if(isLoading){
                    layer.load(1)          //在请求未响应之前,给用户一个等待状态
                }
                var promise = new Promise((resolve, reject)=>{
                    $.ajax({
                        url: baseUrl + url,
                        type: 'GET',
                        async: false,
                        data: params,
                        headers,
                        success: res=>{
                            var res = JSON.parse(res);
                            
                            if(isLoading){
                                layer.closeAll('loading')
                            }

                            if(res.code == 200){
                                resolve(res.data)
                            }else if(res.code == 404){
                                layer.msg(res.msg, {icon: 7})
                                reject(res.data)
                            }else if(res.code == 401){
                                layer.msg('登录过期，请重新登录', {icon: 7})
                                setTimeout(() => {
                                    sessionStorage.clear();
                                    window.location.href = '../page/login-3.html'        //根据后端返回code,判断一些错误情况
                                }, 1500);
                            }else if(res.code == 403){
                                layer.msg('您无权限访问此系统', {icon: 7})
                                setTimeout(() => {
                                    sessionStorage.clear();
                                    window.location.href = '../page/login-3.html'
                                }, 1500);
                            }
                        },
                        error: err=>{
                            layer.msg(err, {icon: 7})       //其他错误情况
                            reject(err)
                        }
                    })
                })

                promise.then(res => {
                    console.log(res);

                    return res;
                },err =>{
                    console.log(err);

                    return err;
                })

                return promise
            },

            /**get请求数据 */
            getData: function (url = '', params = {}, isLoading = false) {
                if(isLoading){
                    layer.load(1)          //在请求未响应之前,给用户一个等待状态
                }

                let resJoson = '';
                
                $.ajax({
                    url: baseUrl + url,
                    type: 'GET',
                    async: false,
                    data: params,
                    headers,
                    success: res=>{
                        resJoson = res;

                        if(isLoading){
                            layer.closeAll('loading')
                        }

                        if(res.code == 200){
                            if (res.result.token){
                                sessionStorage.setItem("token", res.result.token);
                            }
                        }else if(res.code == 404){
                            layer.msg(res.msg, {icon: 7})
                        }else if(res.code == 401){
                            layer.msg('登录过期，请重新登录', {icon: 7})
                            setTimeout(() => {
                                sessionStorage.removeItem('token')
                                window.location.href = '../page/login-3.html'        //根据后端返回code,判断一些错误情况
                            }, 1500);
                        }else if(res.code == 403){
                            layer.msg('您无权限访问此系统', {icon: 7})
                            setTimeout(() => {
                                sessionStorage.removeItem('token')
                                window.location.href = '../page/login-3.html'
                            }, 1500);
                        }else{
                            layer.msg('登录过期，请重新登录', {icon: 7})
                            setTimeout(() => {
                                sessionStorage.removeItem('token')
                                window.location.href = '../page/login-3.html'
                            }, 1500);
                        }
                    },
                    error: err=>{
                        layer.msg(err, {icon: 7})       //其他错误情况
                    }
                })
                
                return resJoson;
            },

            /**post请求数据 */
            postData0: function (url = '', params = {}, isLoading = false) {
                if(isLoading){
                    layer.load(1)
                }
                var promise = new Promise((resolve, reject)=>{
                    $.ajax({
                        url: baseUrl + url,
                        type: 'POST',
                        async: false,
                        data: params,
                        headers,
                        success: res=>{
                            // console.info('res = ', res, 'isLoading =', isLoading);
                            var res = JSON.parse(res);

                            if(isLoading){
                                layer.closeAll('loading')
                            }

                            if(res.code == 200){
                                // console.info('res.code = ', res.code);
                                // console.info('res.data = ', res.data);

                                resolve(res.data)
                            }else if(res.code == 404){
                                layer.msg(res.msg, {icon: 7})
                                reject(res.data)
                            }else if(res.code == 401){
                                layer.msg('登录过期，请重新登录', {icon: 7})
                                setTimeout(() => {
                                    sessionStorage.removeItem('token')
                                    window.location.href = '../page/login-3.html'
                                }, 1500);
                            }else if(res.code == 403){
                                layer.msg('您无权限访问此系统', {icon: 7})
                                setTimeout(() => {
                                    sessionStorage.removeItem('token')
                                    window.location.href = '../page/login-3.html'
                                }, 1500);
                            }
                        },
                        error: err=>{
                            layer.msg(err, {icon: 7})
                            reject(err);
                        }
                    })
                })

                promise.then(res => {
                    console.log(res);

                    return res;
                },err =>{
                    console.log(err);

                    return err;
                })

                // console.info('promise = ', promise, 'promise.PromiseResult = ', promise.PromiseResult);
                // return promise;
            },

            /**post请求数据 */
            postData: function (url = '', params = {}, isLoading = false) {
                if(isLoading){
                    layer.load(1)
                }

                var resJoson = '';

                $.ajax({
                    url: baseUrl + url,
                    type: 'POST',
                    async: false,
                    data: params,
                    headers,
                    success: res=>{
                        resJoson = res;
console.info('resJoson = ', resJoson);
                        if(isLoading){
                            layer.closeAll('loading')
                        }

                        if(res.code == 200){
                            if (res.result.token){
                                sessionStorage.setItem("token", res.result.token);
                            }
                        }else if(res.code == 0){
                            // layer.msg(res.msg, {icon: 6})
                        }else if(res.code == 404){
                            layer.msg(res.msg, {icon: 7})
                        }else if(res.code == 400){
                            layer.msg('数据获取失败', {icon: 7})
                            /*setTimeout(() => {
                                window.history.go(-1);
                            }, 1500);*/
                        }else if(res.code == 401 || res.code == 500){
                            layer.msg('登录过期，请重新登录', {icon: 7})
                            setTimeout(() => {
                                sessionStorage.removeItem('token')
                                window.location.href = '../page/login-3.html'
                            }, 1500);
                        }else if(res.code == 403){
                            layer.msg('您无权限访问此系统', {icon: 7})
                            setTimeout(() => {
                                sessionStorage.removeItem('token')
                                window.location.href = '../page/login-3.html'
                            }, 1500);
                        }else{
                            // layer.msg(res.msg, {icon: 7})
                        }
                    },
                    error: err=>{
                        layer.msg(err, {icon: 7})
                    }
                })

                return resJoson;
            }
        }
        
    exports("miniRequest", miniRequest);

});